<?php

function reequipment_userapi_getrel($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['rel_eq'];
    $join = $xartable['roles'];

    $query = "SELECT  re.user_id, r.xar_name, re.company_id, re.company_name"
    		. " FROM $table re"
    		. " LEFT OUTER JOIN $join r"
    		. " on re.user_id = r.xar_uid";
    $where = array();
    $bind = array();

    if (!empty($user_id)) {
        $where[] = 're.user_id = ?';
        $bind[] = (int)$user_id;
    }
    
    if (!empty($company_id)) {
        $where[] = 're.company_id = ?';
        $bind[] = (int)$company_id;
    }

    if (!empty($company_name)) {
        $where[] = 're.company_name = ?';
        $bind[] = (string)$company_name;
    }
    

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY re.company_id";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($user_id, $xar_name, $company_id, $company_name) = $result->fields;

        $tags[] = array(
                'user_id'       => $user_id,
                'user_name'     => xarVarPrepForDisplay($xar_name),
                'company_id'	=> $company_id,
                'company_name'  => xarVarPrepForDisplay($company_name)
        );
    }

    $result->Close();
    return $tags;
}
?>